package com.ayoka.parenting.ui.group.detail;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.CircleGroupBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.bean.TopicBean;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.ayoka.parenting.widget.TopicOperatorPopupWindow;
import com.jincp.common.dialog.DialogHelper;
import com.jincp.common.utils.UIUtils;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GroupDetailPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GroupDetailPresenter extends BasePresenterImpl<GroupDetailContract.View> implements GroupDetailContract.Presenter {

    private GroupDetailModel model;

    public GroupDetailPresenter() {
        model = new GroupDetailModel();
    }

    @Override
    public void getCircleGroupDetail(String circleGroupId) {
        model.getCircleGroupDetail(this, circleGroupId, new SimpleRequestCallback<CircleGroupBean>() {
            @Override
            public void onSuccess(CircleGroupBean data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configCircleGroupDetail(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
            }
        });
    }

    @Override
    public void getCircleGroupTopicListData(boolean isShowWaitDialog, String circleGroupId, int pageNo, int pageSize) {
        if (isShowWaitDialog) {
            mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        }
        model.getCircleGroupTopicListData(this, circleGroupId, pageNo, pageSize, new SimpleRequestCallback<PagerBean<TopicBean>>() {

            @Override
            public void onSuccess(PagerBean<TopicBean> data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configCircleGroupTopicListData(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
            }
        });
    }

    @Override
    public void showCircleOperatorPopupWindow(final Activity activity, View view, String GroupOwnerId, final TopicBean topicBean, final int operatorPosition) {
        if (view == null && !isAttached()) return;
        // 设置删除和置顶的popupWindow
        final TopicOperatorPopupWindow operatorPopupWindow = new TopicOperatorPopupWindow(activity);
        if (TopicBean.isCircleGroupOwnerId(GroupOwnerId)) {
            operatorPopupWindow.addAction("删除");
            //判断是否已置顶
            if (topicBean.IsTop == 1) {
                operatorPopupWindow.addAction("取消置顶");
            } else {
                operatorPopupWindow.addAction("置顶");
            }

        } else if (TopicBean.isTopicOwner(topicBean.Uid)) {
            operatorPopupWindow.addAction("删除");
        }
        operatorPopupWindow.update();
        operatorPopupWindow.setItemClickListener(new TopicOperatorPopupWindow.OnItemClickListener() {
            @Override
            public void onItemClick(String item, int position) {
                operatorPopupWindow.dismiss();
                switch (position) {
                    case 0:// 删除
                        DialogHelper.getOkCancelDialog(activity, "提示", "删除后不能恢复， 确定要删除吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                deleteTopic(topicBean.Id, operatorPosition);
                            }
                        }).show();
                        break;
                    case 1:// 置顶
                        if (topicBean.IsTop == 1) {
                            //取消置顶
                            cancelTopTopic(topicBean.Id, operatorPosition);
                        } else {
                            topTopic(topicBean.Id, operatorPosition);
                        }
                        break;
                    default:
                        break;
                }
            }
        });
        operatorPopupWindow.showPopupWindow(view);
    }

    private void deleteTopic(String topicId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.deleteTopic(this, topicId, new SimpleRequestCallback<BaseResponse>() {

            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onDeleteTopicCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onDeleteTopicCallback(false, position);
            }
        });
    }

    private void topTopic(String topicId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.topTopic(this, topicId, new SimpleRequestCallback<BaseResponse>() {

            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onTopTopicCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onTopTopicCallback(false, position);
            }
        });
    }

    private void cancelTopTopic(String topicId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.cancelTopTopic(this, topicId, new SimpleRequestCallback<BaseResponse>() {

            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onCancelTopTopicCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onCancelTopTopicCallback(false, position);
            }
        });
    }

    @Override
    public void joinCircle(String circleGroupId) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.joinCircle(this, circleGroupId, new SimpleRequestCallback<BaseResponse>() {

            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onJoinCircleCallback(true);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onJoinCircleCallback(false);
            }
        });
    }

    @Override
    public void quitCircle(String circleGroupId) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.quitCircle(this, circleGroupId, new SimpleRequestCallback<BaseResponse>() {

            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onQuitCircleCallback(true);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onQuitCircleCallback(false);
            }
        });
    }

    @Override
    public void praiseTopic(String topicId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.praiseTopic(this, topicId, new SimpleRequestCallback<BaseResponse>() {

            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onPraiseTopicCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onPraiseTopicCallback(false, position);
            }
        });
    }

    @Override
    public void cancelPraiseTopic(String topicId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.cancelPraiseTopic(this, topicId, new SimpleRequestCallback<BaseResponse>() {

            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onCancelPraiseTopicCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onCancelPraiseTopicCallback(false, position);
            }
        });
    }
}
